// 1. 定义路由组件.

import { createRouter, createWebHistory } from "vue-router";
import Login from "../pages/Login/index.vue";
import Home from "../pages/Home/index.vue";
import NotFound from "../pages/404/index.vue";
// 首页
import homePage from "../pages/Home/homePage/index.vue";

// 车次管理
import TrainNumberManagement from "../pages/Home/TrainNumberManagement/index.vue";

// 车站管理
import transtation from "../pages/Home/stationManagement/index.vue";

// 线路管理
import LineManagement from "../pages/Home/LineManagement/index.vue";

// 车票管理
import TicketManagement from "../pages/Home/TicketManagement/index.vue";

// 订单管理
import orderManagement from "../pages/Home/orderManagement/index.vue";

// 个人轨迹监控
import PersonalTrajectoryMonitoring from "../pages/Home/PersonalTrajectoryMonitoring/index.vue";
// 2. 定义一些路由
// 每个路由都需要映射到一个组件。
// 我们后面再讨论嵌套路由。
export const childRouter = [
  { path: "/home", redirect: "/home/homePage" },
  {
    name: "首页",
    icon: "../../assets/Icons/png/首页.png",
    selectIcon: "../../assets/Icons/png/首页 2.png",
    path: "/home/homePage",
    component: homePage,
  },
  {
    name: "车次管理",
    icon: "../../assets/Icons/png/车次管理.png",
    selectIcon: "../../assets/Icons/png/车次管理 2.png",
    path: "/home/trainNumberManagement",
    component: TrainNumberManagement,
  },
  {
    name: "车站管理",
    icon: "../../assets/Icons/png/车站管理.png",
    selectIcon: "../../assets/Icons/png/车站管理 2.png",
    path: "/home/transtation",
    component: transtation,
  },
  {
    icon: "../../assets/Icons/png/线路管理.png",
    selectIcon: "../../assets/Icons/png/线路管理 2.png",
    name: "线路管理",
    path: "/home/lineManagement",
    component: LineManagement,
  },
  {
    name: "车票管理",
    icon: "../../assets/Icons/png/车票管理.png",
    selectIcon: "../../assets/Icons/png/车票管理 2.png",
    path: "/home/ticketManagement",
    component: TicketManagement,
  },
  {
    name: "订单管理",
    icon: "../../assets/Icons/png/订单管理.png",
    selectIcon: "../../assets/Icons/png/订单管理 2.png",
    path: "/home/orderManagement",
    component: orderManagement,
  },
  {
    name: "个人轨迹监控",
    icon: "../../assets/Icons/png/轨迹管理.png",
    selectIcon: "../../assets/Icons/png/轨迹管理 2.png",
    path: "/home/personalTrajectoryMonitoring",
    component: PersonalTrajectoryMonitoring,
  },
];
const routes = [
  {
    path: "/",
    redirect: "/home",
  },
  {
    path: "/login",
    name: "Login",
    component: Login,
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    children: childRouter,
    // childRouter
  },
  {
    path: "/*",
    redirect: "/notfound",
  },
  {
    path: "/notfound",
    component: NotFound,
  },
];

// 3. 创建路由实例并传递 `routes` 配置
// 你可以在这里输入更多的配置，但我们在这里
// 暂时保持简单
export const router = createRouter({
  // 4. 内部提供了 history 模式的实现。为了简单起见，我们在这里使用 hash 模式。
  history: createWebHistory(),
  routes, // `routes: routes` 的缩写
});
router.beforeEach((to, from, next) => {
  console.log(from);
  
  let TOKEN = localStorage.getItem("token");
  if (to.name !== "Login" && !TOKEN) next({ name: "Login" });
  else next();
});
